const { json } = require('express');
var express = require('express');
var router = express.Router();


let student = require('../src/service/stuSer');


//分页查询
router.get('/getstu', async function (req, res, next) {
  let { current, pageSize,type,value } = req.query;

  current=current||1;
  pageSize=pageSize||10;
  let result = await student.getStudentInfoByPage(current, pageSize,type,value );
  res.json({ code: 0, ...result });
});

//新增学生
router.post('/addstu', async function (req, res, next) {
  let { name, age, gender, address, classId } = req.body;
  let result = await student.stuAdd({ name, age, gender, address, classId });
  if (result) {
    res.json('添加成功');
  } else {
    res.json('添加失败');
  }
});

//删除学生
router.delete('/removestu', async function (req, res, next) {
  let { stuId } = req.body;
  let result = await student.stuDelete({ _id: stuId });
  console.log(req.body, result);
  //{ acknowledged: true, deletedCount: 1 }
  console.log(result);
  if (result.deletedCount == 1) {
    res.json('删除成功');
  } else {
    res.json('删除失败');
  }
});

//批量删除学生
router.delete('/batchStu/:arr', async function (req, res, next) {
  let { arr } = req.params;
  let result = await student.stuRem(JSON.parse(arr));
  if (result.deletedCount == JSON.parse(arr).length) {
    res.json('删除成功');
  } else {
    res.json('删除失败');
  }
});

//修改学生
router.put('/xiustu/:stuId', async function (req, res, next) {

  let { stuId } = req.params;
  let result = await student.stuUpdata(stuId, req.body);
  res.json(result);
});

//id查询学生
router.post('/chastu/:stuId', async function (req, res, next) {
  let { stuId } = req.params;
  let result = await student.stuChaXiang(stuId);
  res.json({ code: 0, result });
});


//用户选择类型查询学生
router.post('/typestu', async function (req, res, next) {
  // delete req.body.page;
  // delete req.body.limit;
  let result = await student.querys(req.body);
  res.json({ code: '0', result });

});







module.exports = router;
